package com.taxi.driver.common;

import android.app.Activity;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;

import com.qianxx.network.RequestError;
import com.qianxx.utils.ToastUtil;
import com.taxi.driver.R;
import com.taxi.driver.common.i.IBasePresenter;
import com.taxi.driver.common.i.IBaseView;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.module.login.LoginActivity;
import com.taxi.driver.widget.dialog.ConfirmDialog;

import rx.subscriptions.CompositeSubscription;

/**
 * 功能描述：基础Presenter
 */
public abstract class BasePresenter implements IBasePresenter {

    protected CompositeSubscription mSubscriptions = new CompositeSubscription();
    protected boolean mFirstSubscribe = true;
    private ConfirmDialog mDialog;

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        mFirstSubscribe = false;
        mSubscriptions.clear();
    }

    protected void showNetworkError(Throwable e, String defErrorMsg, IBaseView view) {
        if (!showNetworkError(e, view, null)) {
            view.toast(defErrorMsg);
        }
    }

    protected void showNetworkError(Throwable e, @StringRes int defErrorMsg, IBaseView view) {
        if (!showNetworkError(e, view, null)) {
            view.toast(defErrorMsg);
        }
    }

    protected void showNetworkError(Throwable e, @StringRes int defErrorMsg, IBaseView view, UserRepository userRepository) {
        if (!showNetworkError(e, view, userRepository)) {
            view.toast(defErrorMsg);
        }
    }

    private boolean showNetworkError(Throwable e, IBaseView view, UserRepository userRepository) {
        view.hideLoadingView();
        Throwable throwable = e;
        while (throwable.getCause() != null) {
            throwable = throwable.getCause();
        }
        if (throwable instanceof RequestError) {
            if (((RequestError) throwable).getErrCode() == 10011 && userRepository != null) {//登录过期
                Activity activity = null;
                if (view instanceof Activity) {
                    activity = (Activity) view;
                } else if (view instanceof Fragment) {
                    activity = ((Fragment) view).getActivity();
                }
                view.toast(throwable.getMessage());
                logout(userRepository, activity);
                if (activity != null) {
                    activity.finish();
                }
                return true;
            } else if (((RequestError) throwable).getErrCode() == 10012 && userRepository != null) {//封号
                Activity activity = null;
                if (view instanceof Activity) {
                    activity = (Activity) view;
                } else if (view instanceof Fragment) {
                    activity = ((Fragment) view).getActivity();
                }
                showDialog(activity, throwable.getMessage(), userRepository);
                return true;
            } else {
                view.toast(throwable.getMessage());
            }
            return true;
        }
        if ("timeout".equals(e.getMessage())) {
            view.toast(R.string.network_timeout);
            return true;
        }

        return false;
    }

    private void showDialog(Activity activity, String title, UserRepository userRepository) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        mDialog = new ConfirmDialog(activity, title, null, "确定");
        mDialog.setConfirmClickListener(sweetAlertDialog -> {
            sweetAlertDialog.dismiss();
            logout(userRepository, activity);
            activity.finish();
        });
        mDialog.show();
    }

    private void logout(UserRepository userRepository, Activity activity) {
        userRepository.logout();
        LoginActivity.start(activity);
    }

    protected void toast(@StringRes int resId) {
        ToastUtil.getInstance().toast(resId);
    }

    protected void toast(String msg) {
        ToastUtil.getInstance().toast(msg);
    }

    protected void showNetworkError(Throwable e, String defErrorMsg) {
        if (!showNetworkError(e)) {
            toast(defErrorMsg);
        }
    }

    protected void showNetworkError(Throwable e, @StringRes int defErrorMsg) {
        if (!showNetworkError(e)) {
            toast(defErrorMsg);
        }
    }

    private boolean showNetworkError(Throwable e) {
        Throwable throwable = e;
        while (throwable.getCause() != null) {
            throwable = throwable.getCause();
        }
        if (throwable instanceof RequestError) {
            toast(throwable.getMessage());
            return true;
        }
        if ("timeout".equals(e.getMessage())) {
            toast(R.string.network_timeout);
            return true;
        }
        return false;
    }
}
